# this is run in a chrooted environment
import glob
from os.path import isdir, basename, exists, join
import os

from forest.api import get_tool

#localdirnames = set()
#for localdir in glob.glob('/usr/share/locale/*'):
#    if not isdir(localdir):
#      continue
#    localdirnames.add(basename(localdir))
#removelocales = localdirnames.difference(set(config.get('locales')))
#print sorted(list(removelocales))
#for local in removelocales:
#    print 'Removing','/usr/share/locale/{0}'.format(local)
#    yum.get_tool('system.remove')('/usr/share/locale/{0}'.format(local))


#for package in config.get('remove_locales_from', []):
#      yum.removefrom(package, '/usr/share/locale/*')

#class Dummy(object):
#    pass


import subprocess

# the nice way
localdefout = subprocess.check_output(['localedef','--list-archive']).split('\n')

# the brute way
localdefout = os.listdir('/usr/share/locale')
#print "localedefout: ", localdefout
remove = []
for locale in localdefout:
    locale= locale.strip()

    if not locale:
        continue

    if locale not in config.get('locales'):
        remove.append(locale)

#print "locales to remove:", remove

# the nice way
for r in remove:
    subprocess.check_call(['localedef','--delete-from-archive',r])

get_tool('system.remove')('/usr/lib/locale/locale-archive')

# the brute way
for r in remove:
    get_tool('system.remove')('/usr/share/locale/%s' % r)


localdefout = os.listdir('/usr/share/i18n/locales')
#print "localedefout: ", localdefout
remove = []
for locale in localdefout:
    locale= locale.strip()

    if not locale:
        continue

    if locale not in config.get('locales'):
        remove.append(locale)

for r in remove:
    get_tool('system.remove')('/usr/share/i18n/locales/%s' % r)

# the nice way
#if exists('/usr/lib/locale/locale-archive'):
#    get_tool('system.move')('/usr/lib/locale/locale-archive', '/usr/lib/locale/locale-archive.tmpl')
#print subprocess.check_output(['/usr/sbin/build-locale-archive'])

for kmodpath in config.get('remove_kmods', []):
    get_tool('system.remove')('/lib/modules/*/kernel/%s' % kmodpath)

#get_tool('system.remove')('/lib/firmware/*')
get_tool('system.remove')('/usr/share/doc/*')
get_tool('system.remove')('/usr/share/man/*')
get_tool('system.remove')('/usr/share/grub')
get_tool('system.remove')('/home')

#if os.path.exists('/boot'):
#    get_tool('system.remove')('/boot')

get_tool('system.remove')('/lib/modules/*/kernel/drivers/isdn')
get_tool('system.remove')('/lib/modules/*/kernel/drivers/media')
get_tool('system.remove')('/lib/modules/*/kernel/drivers/net/wireless')

# remove big unnecessary firmware
get_tool('system.remove')('/lib/firmware/bnx2x')
get_tool('system.remove')('/lib/firmware/ti-connectivity')
get_tool('system.remove')('/lib/firmware/ueagle-atm')
get_tool('system.remove')('/lib/firmware/libertas')
get_tool('system.remove')('/lib/firmware/bnx2')
get_tool('system.remove')('/lib/firmware/xvge')
get_tool('system.remove')('/lib/firmware/ath6k')
get_tool('system.remove')('/lib/firmware/radeon')
get_tool('system.remove')('/lib/firmware/libertas')
get_tool('system.remove')('/lib/firmware/vxge')

get_tool('system.remove')('/usr/lib64/python*/lib2to3')

# remove large unused codecs
get_tool('system.remove')('/usr/lib*/python*/lib-dynload/_codecs_tw.so')
get_tool('system.remove')('/usr/lib*/python*/lib-dynload/_codecs_kr.so')
get_tool('system.remove')('/usr/lib*/python*/lib-dynload/_codecs_cn.so')
get_tool('system.remove')('/usr/lib*/python*/lib-dynload/_codecs_hk.so')
#get_tool('system.remove')('/usr/lib*/python*/lib-dynload/_bsddb.so')
get_tool('system.remove')('/usr/lib*/python*/lib-dynload/_codecs_jp.so')

#
##yum.get_tool('system.remove')('/usr/lib/locale/locale-archive')
#
#if False:
#    # remove docs
#    yum.get_tool('system.remove')('/usr/share/doc /usr/share/info /usr/share/man /usr/share/gnome'.split(' '))
#
#
#    yum.get_tool('system.remove')('/usr/lib64/python*/idlelib')
#
#    # remove extra charsets
#    charsetnames = set()
#
#    for charsetfile in glob.glob('/usr/lib64/python*/encodings/*'):
#        if not basename(charsetfile).startswith('utf'):
#          continue
#        charsetnames.add(basename(charsetfile))
#
#    for charset in charsetnames:
#        yum.get_tool('system.remove')('/usr/share/i18n/charmaps/{0}'.format(charset))
#

#
#    def cleanup_gconv():
#        charsetnames = set()
#        for charsetfile in glob.glob('/usr/lib64/gconv/*'):
#            if basename(charsetfile) == 'UTF-8.so':
#              continue
#            charsetnames.add(basename(charsetfile))
#        for charset in charsetnames:
#            yum.get_tool('system.remove')('/usr/lib64/gconv/{0}'.format(charset))
#
#
#
#    #yum.get_tool('system.remove')('/usr/lib64/libicu*')
#

#
#    import subprocess
#
#    localdefout = subprocess.check_output(['localedef','--list-archive'])
#
#    remove = []
#    for locale in localdefout.split('\n'):
#        locale= locale.strip()
#
#        if not locale:
#            continue
#
#        if locale not in 'en_US.utf8':
#            remove.append(locale)
#
#    for r in remove:
#        print subprocess.check_call(['localedef','--delete-from-archive',r])
#
#    if exists('/usr/lib/locale/locale-archive'):
#        get_tool('system.move')('/usr/lib/locale/locale-archive', '/usr/lib/locale/locale-archive.tmpl')
#
#    print subprocess.check_output(['/usr/sbin/build-locale-archive'])
#

    #yum.get_tool('system.remove')('/boot/*')

    #
    #
    #
    #
    #
    #
    #
    #
    #yum.get_tool('system.remove')('/usr/lib64/python*/lib-dynload/unicodedata.so')
    #yum.get_tool('system.remove')('/usr/lib64/python*/lib-dynload/unicodedata.so')



    # large files
'''
/var/lib/rpm
/var/lib/yum

This other trick is reducing (as opposed to destroying) the “locale-archive” file. Saves about 75 uncompressed Mbytes (found in RedHat’s Thincrust ace/resources/cobbler/cobbler.ks)

%post
    '''
